package com.example.moviegroup.activity;
import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.example.moviegroup.R;
import com.example.moviegroup.fragment.HeadLineFragment;
import com.example.moviegroup.fragment.MineFragment;
import com.example.moviegroup.fragment.MovieFragment;


/*****************************
 *
 * 名字：主Activity
 *
 * 步骤：1.添加fragment
 *
 * 作者：xiaodong
 *
 * 时间：2015-11-2 ,下午9:45:27
 ****************************/
public class MainActivity extends FragmentActivity {
    ArrayList<Fragment> list = new ArrayList<Fragment>();

    RadioGroup rg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        initdata();

    }

    private void setListener() {
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = getSupportFragmentManager()
                        .beginTransaction();
                for (int i = 0; i < rg.getChildCount(); i++) {
                    int id = rg.getChildAt(i).getId();
                    System.out.println(id + "," + checkedId);
                    if (id == checkedId) {
                        transaction.replace(R.id.content, list.get(i));
                        transaction.commit();
                    }
                }
            }
        });
    }

    private void initView() {
        list.add(new HeadLineFragment());
        list.add(new MovieFragment());
        list.add(new MineFragment());
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.content, list.get(0));
        transaction.commit();
        rg = (RadioGroup) findViewById(R.id.radioGroup1);

    }

    private void initdata() {

    }

}
